/*
 * @Author: qq417045639
 * @Date: 2022-03-04 21:18:54
 * @LastEditors: qq417045639
 * @LastEditTime: 2022-03-26 09:51:49
 * @FilePath: /lib/uniCloud.js
 * @Description: In User Settings Edit
 */

/*
 需配置项
*/
import ud from './foxUniCloud-database.js'
import callUdFunction from './foxUniCloud-function.js'
const isDebug = true //是否开启调试模式 调试会在控制台输出内容
/*
 无须修改
*/
function debugPrint(data) {

	if (isDebug) {
		console.log(data)
	}
}

async function upload(callback){
	//前端代码
	uni.chooseImage({
		count: 1,
		success(res) {
			console.log(res);
			if (res.tempFilePaths.length > 0) {
				let filePath = res.tempFilePaths[0]
				//进行上传操作
	
				// promise方式
				var timestamp = Date.parse(new Date())//当前时间戳
				const result = uniCloud.uploadFile({
					filePath: filePath,
					cloudPath: timestamp+'.jpg',
					onUploadProgress: function(progressEvent) {
				          

						  /*
				          var percentCompleted = Math.round(
				            (progressEvent.loaded * 100) / progressEvent.total
				          );
						  */
				    },
					success:function(res){
						callback(res)
						//上传图片的路径
						//console.log('imgpath',res.filePath);
					}
				});
	
			}
		}
	});
	
}


export default {



	//云数据库
	UD1: {
		tableName: '-1',
		collection: '-1',
		table(tableName) {
			const db = uniCloud.database();
			var creatcollection = db.collection(tableName);
			var ud = UD();
			ud.collection = creatcollection;
			return ud;
		},
		find(where = {}) {
			if (this.collection == '-1') {
				debugPrint('未指定数据库表!');
			}

			collection.where(where).get().then(function(res) {
				debugPrint(res.result.data);
			})
		},
		select() {
			this.find()
		},

		del(_id) {
			const db = uniCloud.database();
			const collection = db.collection(tableName);
			collection.remove(_id)
		},
		p() {
			ddebugPrint(tableName);
		}
	},
	install(Vue) {
		//云函数
		Vue.prototype.UF = callUdFunction
		
		//云数据库
		Vue.prototype.UD = ud
		
		Vue.prototype.upload = upload
		
	}



}
